//
//  RegistController.swift
//  SWfanghu
//
//  Created by 赵宇桥 on 16/6/7.
//  Copyright © 2016年 ly. All rights reserved.
//

import UIKit

class RegistController: BaseviewController,UITableViewDelegate,UITableViewDataSource {
    // MARK: property
    var tableview:UITableView!
    var diquBT:UIButton!
    var registBT:UIButton!
    var fasong:UIButton!
    var time = 60
    public var cityInt:Int = 0
    var titles:[[String]]{
    return[
        ["所在地","姓名"],
        ["手机号","验证码","邀请码"],
        ["设置密码"]
        ]
    }
    
    var icons:[[String]]{
        return[
            ["suozaidi.png","name.png"],
            ["login_phone.png","yanzhengma.png","mima.png"],
            ["mima.png"]
        ]
    }
    var placeholders:[[String]]{
        return[
            ["所在地","名字"],
            ["电话号","验证码","密码"],
            ["邀请码"]
        ]
    }
    let citys = ["北京","上海","铁岭","石家庄"]
    var textfields:[UITextField] = []
    
    // MARK: life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "注册"

        let tableHeight = 88 * 6 + 16 * 3 + navbarHeight
        tableview = UITableView(frame: CADipRectMake(0, 0, frameSize.width, tableHeight), style: .Plain)
        tableview.delegate = self
        tableview.dataSource = self
        tableview.backgroundColor = UIColor.clearColor()
        tableview.tableFooterView = UIView(frame: CGRectZero)
        tableview.bounces = false
        tableview.allowsSelection = false
        let ges = UITapGestureRecognizer(target: self, action: #selector(RegistController.hideKeyboar))
        tableview.addGestureRecognizer(ges)
        view.addSubview(tableview)

        tableview.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        
        registBT = addButton(CADipRectMake(spaceHeight, tableHeight + 60, frameSize.width - 2 * spaceHeight, 88), tittle: "完成", target: self, action: #selector(RegistController.buttonClick(_:)))
        view.addSubview(registBT)
        NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(RegistController.update(_:)), userInfo: nil, repeats: true).fire()
        
    }
    override func viewDidAppear(animated: Bool) {
        textfields[0].text = citys[cityInt]
    }
    //MARK: - 
    func hideKeyboar() {
        view.endEditing(true)
    }
    func update(dt:Float){
        guard fasong != nil else{return}
        time -= 1
        if time < 60 {
            fasong.enabled = false
            fasong.setTitle("\(time)秒后重发", forState: .Disabled)
        }else{
            time = 60
            fasong.enabled = true
            fasong.setTitle("重新发送验证码", forState: .Normal)
        }
    }
    func buttonClick(sender:UIButton){
        print("zhuce")
        switch sender {
//        case registBT:
//            break
//        case fasong:
//            break
        case diquBT:
            hideKeyboar()
            let vc = AreaTableviewController()
            vc.delegate = self
            self.navigationController?.pushViewController(vc, animated: true)
        default:
            break
        }
    }
    // MARK: Tableview Delegate
    func numberOfSectionsInTableView(tableView: UITableView) -> Int{
       return titles.count
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return titles[section].count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = tableview.dequeueReusableCellWithIdentifier("cell")!
        
        let icon = UIImageView()
        icon.image = UIImage(named: icons[indexPath.section][indexPath.row])
        icon.frame = CADipRectMake(spaceHeight, (88-40) / 2, 40, 40)
        cell.addSubview(icon)
        
        let text = UITextField(frame: CADipRectMake(spaceHeight*2+40, 0, frameSize.width-spaceHeight*2+40, 88))
        text.placeholder = placeholders[indexPath.section][indexPath.row]
        cell.addSubview(text)
        textfields.append(text)
        
        if indexPath.section == 0 {
            diquBT = UIButton(type: .Custom)
            diquBT.frame = CADipRectMake(0, 0, frameSize.width, 88)
            diquBT.addTarget(self, action: #selector(RegistController.buttonClick(_:)), forControlEvents: .TouchUpInside)
            textfields[0].addSubview(diquBT)
        }
        
        if indexPath.section == 1 && indexPath.row == 1 {
            fasong = UIButton(type: .Custom)
            let w = getStringWidthBy("重新发送验证码", font: (fasong.titleLabel?.font.pointSize)!)
            fasong.frame = CADipRectMake(frameSize.width - spaceHeight - w, 0, w, 88)
            fasong.setTitle("重新发送验证码", forState: .Normal)
            fasong.setTitleColor(UIColor.redColor(), forState: .Normal)
            cell.addSubview(fasong)
            
            let line = UIView(frame: CADipRectMake(frameSize.width - spaceHeight - w - spaceHeight, 0, 2, 88))
            line.backgroundColor = color_line
            cell.addSubview(line)
        }
        
      return cell
    }
    
    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
        view.tintColor = UIColor.clearColor()
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
        let h = CADipRectMake(0, 0, 0, 88)
        return h.height
    }
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
        let h = CADipRectMake(0, 0, 0, 16)
        return h.height
    }
    
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat{
        return 0
    }
    
}